package com.example.diary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "NotePad.db";
    public static final int VERSION = 1;
    public static final String TABLE_NAME = "Diary";
    public static final String TABLE_NAME_USER = "User";
    //建表语句
    public static final String CREATE_DIARY = "create table Diary(" +
            "id integer primary key autoincrement," +
            "title text," +
            "time text," +
            "author text," +
            "content text," +
            "picture BLOB)";

    // todo 用户主键辨识,与登录用户保持一致
    public static final String CREATE_USER = "create table User(" +
            "id integer primary key autoincrement," +
            "name text," +
            "password text," +
            "phone text)";

    private Context mContext;

    public DatabaseHelper(Context context, String name,
                          SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_DIARY);
        // 创建默认数据库
        db.execSQL("insert into Diary(id,title,time,author,content,picture) values('1','第一篇日记','2021-01-01','admin','第一篇日记',null)");
        db.execSQL(CREATE_USER);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
